namespace hjc {

	export class LaunchManager extends Singleton implements ILaunchable {

		private $lastCount: number = 0;
		public launch(options: hjc.LaunchOptions): void {
			this._configurate(options);
			this._launchAll(options);
			// hjc.SceneManager.instance.firstEnter();
			// if (lp.eg) game.eg = effect.Generator.instance;
			// let wsParams = game.initParams.websocket
			// if (notEmpty(wsParams)) {
			// 	game.ws = tools.websocket.instance;
			// 	if (wsParams.url) game.ws.url = wsParams.url;
			// 	else {
			// 		game.ws.host = wsParams.host
			// 		game.ws.port = wsParams.port;
			// 	}
			// 	game.ws.dispatcher = new wsParams.dispatcher();
			// }
		}

		private _launchAll(options: hjc.LaunchOptions): void {
			new UIAdapater().launch(options);
			if (!!options.ws) game.ws.launch(options);
			game.manager = {
				layer: LayerManager.instance<LayerManager>(),
				proxy: ProxyManager.instance<ProxyManager>(),
				mediator: MediatorManager.instance<MediatorManager>(),
				controller: ControllerManager.instance<ControllerManager>(),
				view: ViewManager.instance<ViewManager>(),
				effect: EffectManager.instance<EffectManager>(),
				system: SystemManager.instance<SystemManager>(),
				entity: EntityManager.instance<EntityManager>(),
				time: TimeManager.instance<TimeManager>(),
			}
			Object.values(game.manager).forEach(mgr => {
				if (egret.is(mgr, 'hjc.ILaunchable')) mgr.launch(options);
			});
		}

		private _configurate(options: hjc.LaunchOptions): void {
			if (!numeric(options.configs.width) || !numeric(options.configs.width)) {
				hjc.error('GameConfigs.width & height must be a number!');
				return;
			}
			game.stage = ui.getStage();
			game.frameRate = game.stage.frameRate;
			game.width = options.configs.width;
			game.height = options.configs.height;
			game.showAll = options.configs.showAll ? true : false;
			game.dev = options.configs.dev ? true : false;
			game.test = options.configs.test ? true : false;
			game.loading = options.loading ? options.loading : null;
			if (!!options.configs.ui) {
				if (numeric(options.configs.ui.clickedScale)) game.clickedScale = options.configs.ui.clickedScale;
				if (numeric(options.configs.ui.clickedAlpha)) game.clickedAlpha = options.configs.ui.clickedAlpha;
				if (numeric(options.configs.ui.toggledScale)) game.toggledScale = options.configs.ui.toggledScale;
				if (numeric(options.configs.ui.toggledAlpha)) game.toggledAlpha = options.configs.ui.toggledAlpha;
				if (Math.isPositive(options.configs.ui.aniDuration)) game.aniDuration = options.configs.ui.aniDuration;
			}
			if (!!options.configs.log) {
				let [counted, valved] = [!!options.configs.log.newCount, Math.isPositive(options.configs.log.newCountValve)];
				if (counted || valved) {
					game.timer.registerBySeconds(1, (to: TimeParam) => {
						let count = egret.$hashCount - this.$lastCount;
						if (valved && count >= options.configs.log.newCountValve) hjc.warn(`[${to.times}s] new objects num exceeds valve：${count}`);
						else if (counted) hjc.info(`[${to.times}s] new objects num ：${count}`);
						this.$lastCount = egret.$hashCount;
					});
				}
			}
		}
	}
}